/**
 * FileFind - ftp/shares indexing and search php engine
 * Module: agent-java
 * Description: Agent written in java language
 * Copyright (c) 2003-2010 Rage Steel, www.gt2.ru
 * ----------------------------------------------
 * $Id$
 */
package ru.ffind.agent.io;

import java.io.File;
import java.util.Collection;
import java.util.Collections;

import ru.ffind.agent.model.AbstractListingStorage;
import ru.ffind.agent.model.RootDirectory;

import com.google.common.collect.Lists;

/**
 *
 * Implementation of ListingStorage for java.io.File
 *
 * @author Rage Steel
 * @version $Id$
 *
 */
public class FileListingStorage extends AbstractListingStorage {
	
	private Collection<File> rootDirs;
		
	/**
	 * Constructor for use in file scanner
	 * @param rootDirs
	 */
	public FileListingStorage(File[] rootDirs) {
		this.rootDirs = Collections.synchronizedCollection(Lists.newArrayList(rootDirs));
		put(RootDirectory.ROOT_NAME, RootDirectory.create(FileDirectory.convertFilesToEntries(rootDirs)));
	}
	
	/* (non-Javadoc)
	 * @see ru.ffind.agent.model.IListingStorage#isScanComplete()
	 */
	@Override
	public boolean isScanComplete() {
		
		// Scanning is complete after all root directories removed
		return rootDirs.isEmpty();
	}
	
	/**
	 * 
	 * Called from scanner to indicate completion of scanning for specified root
	 * 
	 * @param root start folder
	 * 
	 */
	public void setFileScanComplete(File root) {
		if (!rootDirs.remove(root)) {
			throw new RuntimeException("Trying to remove non-existing (or already removed) root directory: " + root);
		}
	}
}
